package com.example.demo.thread;

/**
 * 项目名称：hello-6
 * 类 名 称：TestThread2
 * 类 描 述：龟兔赛跑
 * 创建时间：2021/4/24 18:46
 * 创 建 人：wangsheng
 */
public class TestThread2 implements Runnable {
    private static String name = "";

    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {

            boolean flag = running(i);
            if (flag) {
                break;
            }
            if (name == "") {
                System.out.println(Thread.currentThread().getName() + "跑了：" + i + "步");
            }
        }
    }

    Boolean running(int i) {
        if (name != "") {
            return true;
        } else {
            if (i == 100) {
                name = Thread.currentThread().getName();
                System.out.println(Thread.currentThread().getName() + "跑完：" + i + "步");
                System.out.println(Thread.currentThread().getName() + "获胜");
                return true;
            } else {
                return false;
            }
        }
    }

    public static void main(String[] args) {
        TestThread2 testThread2 = new TestThread2();
        new Thread(testThread2, "🐢🐢🐢🐢🐢🐢🐢🐢🐢🐢").start();
        new Thread(testThread2, "🐰🐰🐰🐰🐰🐰🐰🐰🐰🐰").start();

    }
}
